package cdu.javaendwork.Controller.Admin.User;

import cdu.javaendwork.model.sqlModel.User;
import cdu.javaendwork.service.Impl.User.UserServiceImpl;
import cdu.javaendwork.service.Interface.User.UserService;
import cdu.javaendwork.util.pageHelp.PageHelper;
import cdu.javaendwork.util.pageHelp.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/user_select")
public class UserSelectController extends HttpServlet {

    PageHelper pageHelper = new PageHelper();
    UserService userService=new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html;charset = utf-8");
        req.setCharacterEncoding("UTF-8");

        String username = req.getParameter("username");
        int status= Integer.parseInt(req.getParameter("status"));
        String sPage=req.getParameter("page");
        int pageSize=3;
        int pageIndex=(sPage == null || sPage.equals("")) ? 1 : Integer.parseInt(sPage);

        // 判断 管理员 or 用户
        String b=req.getParameter("book");
        System.out.println(b);
        int book=(b == null || b.equals("")) ? 1 : Integer.parseInt(b);

        List<User> userList=userService.filterByUsernameAndStatusLimit(username,status,pageIndex,pageSize);
        PageInfo pageInfo=pageHelper.filter("User", "(status="+status+") and username like '%" + username + "%'",pageIndex,pageSize);

        req.setAttribute("usermanage",userList);
        req.setAttribute("pageInfo",pageInfo);

//        System.out.println(userList);
//        System.out.println(pageInfo);
        req.setAttribute("username" , username);
        req.setAttribute("status" , status);
        req.setAttribute("falg" , new Integer(1));

        // 判断是管理员查询还是普通用户查询
        if(book == 2)
        {
            req.setAttribute("adminList" , userList);
            req.getRequestDispatcher("admin_manage.jsp").forward(req,resp);
            return;
        }

        req.getRequestDispatcher("user_manage.jsp").forward(req,resp);

    }
}
